package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: libin
 * @date: 2019/11/23 11:11
 * @version: 1.0
 * @describe:
 */
@Service(interfaceClass =SetmealService.class )
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;


    /**
     * 分页查询套餐
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findAll(Integer currentPage, Integer pageSize, String queryString) {
        //使用分页查询工具类
        Page page = PageHelper.startPage(currentPage,pageSize);
        //普通sql
        List<Setmeal> Setmeals = setmealDao.findAll(queryString);
        return new PageResult(page.getTotal(),Setmeals);
    }

    /**
     * 添加套餐
     * @param setmeal
     */
    @Override
    public void add(Setmeal setmeal) {
        //添加套餐信息，返回主键id
        setmealDao.add(setmeal);
        Integer setmealId =setmeal.getId();


        //循环建立套餐和检查组的关系表
        List<Integer> checkGroupIds = setmeal.getCheckgroupIds();
        if (CollectionUtil.isNotEmpty(checkGroupIds)) {
            List<Map> maps =new ArrayList<>();
            for (Integer checkGroupId : checkGroupIds) {
                Map map=new HashMap();
                map.put("setmeal_id",setmealId);
                map.put("checkgroup_id",checkGroupId);
                maps.add(map);
            }
            setmealDao.setSetmealAndCheckGroup(maps);
        }

    }

    /**
     * 查询所有套餐
     * @return
     */
    @Override
    public List<Setmeal> getSetmeal() {
        List<Setmeal> setmeals= setmealDao.getSetmeal();
        return setmeals;
    }

    /**
     * 根据id查询套餐详情
     * @param id
     * @return
     */
    @Override
    public Setmeal findByIdBatch(int id) {
        //1.查询套餐基本信息
        Setmeal setmeal = setmealDao.findSetmealById(id);

        if (setmeal != null) {
            //2.查询对应检查组信息
            List<CheckGroup> checkGroups = setmealDao.findCheckGroupById(id);

            if (CollectionUtil.isNotEmpty(checkGroups)) {
                //3.查询对应的检查项
                //3.1获取所有的checkGroups_id
                List<Integer> listIds=new ArrayList<>();
                for (CheckGroup checkGroup : checkGroups) {
                    Integer ids = checkGroup.getId();
                    listIds.add(ids);
                }
                //3.2查找所有检查组的检查项
              List<CheckItem> checkItemList = setmealDao.findCheckItemByCheckGroupIds(listIds);

                //分组
                Map<Integer,List<CheckItem>> map = new HashMap<>();

                for (CheckItem checkItem : checkItemList) {
                    //获取检查组的id
                    Integer checkGroupId = checkItem.getCheckGroupId();
                    //根据检查组的id获取对应的检查项
                    List<CheckItem> checkItems = map.get(checkGroupId);
                    if (checkItems == null) {
                        //List<CheckItem>如果没有，就创建一个
                        checkItems = new ArrayList<>();
                    }
                    checkItems.add(checkItem);
                    map.put(checkGroupId,checkItems);
                }

                //遍历checkGroups集合，将检查项集合取出放入对应检查组
                for (CheckGroup checkGroup : checkGroups) {
                    checkGroup.setCheckItems(map.get(checkGroup.getId()));
                }

                setmeal.setCheckGroups(checkGroups);
            }
        }
        return setmeal;
    }


    /**
     * 订阅中的套餐基本信息
     * @param id
     * @return
     */
    @Override
    public Setmeal findBySetmealId(int id) {
        Setmeal setmeal = setmealDao.findSetmealById(id);
        return setmeal;
    }
}
